package rursu.dal.loaders;

import java.util.List;

import rursu.business.ProgressListener;
import rursu.business.pojo.MovieImage;
import rursu.dal.managers.DataBaseManager;
import rursu.dal.managers.interfaces.IMovieImageDAL;
import rursu.providers.callback.ImageLoaderCallback;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.util.Log;

public class ImageLoader extends AsyncTask<Integer, MovieImage, Integer> {

	private String LOGGER_TAG = ImageLoader.class.getCanonicalName();
	private ImageLoaderCallback callback;
	private ProgressListener progressListener;
	private DataBaseManager dataBaseManager;

	private String imageType;
	private String imageSize;
	private boolean allNeeded = false;

	@Override
	protected Integer doInBackground(Integer... movieIds) {
		
		int totalNumOfImages = 0;
		IMovieImageDAL movieImageDALManager = dataBaseManager.getMovieImageDALManager();		
		
		for (Integer movieId : movieIds) {
			
			List<MovieImage> list = movieImageDALManager.get(movieId, imageSize, imageType);
			
			if ( list == null || list.isEmpty() ) {
				Log.v( LOGGER_TAG, "There are no image data in DB" );
				return null;
			}
			
			for ( MovieImage image : list ) {
				
				String path = movieImageDALManager.getPath(image.getId(), imageSize, imageType);
				Bitmap bitmap = null;
				
				if ( path == null ) {
					
					path = movieImageDALManager.saveImage(image.getUrl());
					
					if ( path != null ) {
						movieImageDALManager.savePath(image.getId(), path);
						bitmap = movieImageDALManager.getBitmap(path);
					}
					
				} else {
					
					bitmap = movieImageDALManager.getBitmap(path);
				}
				
				image.setBitmap(bitmap);
				image.setMovieId(movieId);
				
				publishProgress( image );
				totalNumOfImages ++;
				
				if ( !allNeeded ) {
					break;
				}
			}
		}

		return new Integer(totalNumOfImages);
	}

	@Override
	protected void onCancelled() {
		if ( progressListener != null ) {
			progressListener.progressFinished();
		}
	}
	
	@Override
	protected void onProgressUpdate(MovieImage... values) {
		
		if ( progressListener != null ) {
			progressListener.progressContinue();
		}
		
		callback.onImageLoaded(values[0]);
	}
	
	public ProgressListener getProgressListener() {
		return progressListener;
	}

	@Override
	protected void onPreExecute() {
		
		if ( progressListener != null ) {
			progressListener.progressContinue();
		}
		
	}
	
	@Override
	protected void onPostExecute(Integer result) {
		Log.v(LOGGER_TAG, "task finished");
		if ( progressListener != null ) {
			progressListener.progressFinished();
		}
		
		if ( callback == null || result == null) {
			Log.d(LOGGER_TAG, "ERROR: NullPointer Exception: callback = " + callback + "	result = " + result);
		} else {
			callback.onLoadFinished(result);
		}
		
	}
	
	public void setProgressListener(ProgressListener progressListener) {
		this.progressListener = progressListener;
	}

	public ImageLoaderCallback getCallback() {
		return callback;
	}

	public void setCallback(ImageLoaderCallback callback) {
		this.callback = callback;
	}

	public DataBaseManager getDataBaseManager() {
		return dataBaseManager;
	}

	public void setDataBaseManager(DataBaseManager dataBaseManager) {
		this.dataBaseManager = dataBaseManager;
	}

	public String getImageType() {
		return imageType;
	}

	public void setImageType(String imageType) {
		this.imageType = imageType;
	}

	public String getImageSize() {
		return imageSize;
	}

	public void setImageSize(String imageSize) {
		this.imageSize = imageSize;
	}

	public boolean isAllNeeded() {
		return allNeeded;
	}

	/**
	 * If you need just one image, you should set it to false
	 * otherwise - true
	 * @param allNeeded
	 */
	public void setAllNeeded(boolean allNeeded) {
		this.allNeeded = allNeeded;
	}	

}
